package com.yjs.app.trade.web.controller.business.statistics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.util.DateUtil;
import com.yjs.app.trade.core.util.MongoUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.StatisticsConstant;
import com.yjs.app.trade.service.service.business.order.OrderService;
import com.yjs.app.trade.service.service.sys.UserService;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

/**
 * 发票相关控制层
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/app/business/statistics/seller")
public class SellerStatisticsController extends BaseController {
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	UserService userService;
	
	/**
	 * 近12月配送金额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerOrderTotalPriceTwelveMonths", method = RequestMethod.GET)
	@ResponseBody
	public Object dispatcherOrderTotalPriceTwelveMonths(String data, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String passportId = getUserId(request);
		map.put("sellerId", getUserMemberId(request));
		List<String> projection = new ArrayList<String>();
		projection.add("sellerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("years", 1);
		sorts.put("months", 1);
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId), 
				passportId,
				StatisticsConstant.SellerCollection.SELLERORDERTOTALPRICETWELVEMONTHS);
		List<String> list = MongoUtil.getListInfo(changeCollectionName, map, projection, null, null, sorts);
		List<String> dates = DateUtil.getLastDates(new Date(), 12, Calendar.MONTH, DateUtil.dateFormatMonth);
		String years = "";
		String months = "";
		boolean flag = false;
		for (String string : dates) {
			flag = false;
			for (String json : list) {
				JSONObject jsonObject = JSONObject.fromObject(json);
				if (jsonObject.get("years") != null && jsonObject.get("months") != null) {
					years = jsonObject.getString("years");
					months = jsonObject.getString("months");
					if (StringUtils.equals(string, years + "-" + months) ) {
						flag = true;
					}
				}
			}
			if (!flag) {
				String year = string.split("\\-")[0];
				String month = string.split("\\-")[1];
				list.add("{\"years\":\"" + year + "\", \"months\":\"" + month + "\", \"lastYears\":\"" + (Integer.parseInt(year) - 1) + "\", \"lastMonths\":\"" + month + "\"}");
			}
		}
		list.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				JSONObject jsonObject1 = JSONObject.fromObject(o1);
				JSONObject jsonObject2 = JSONObject.fromObject(o2);
				if (jsonObject1.get("years") != null && jsonObject2.get("years") != null &&
						jsonObject1.get("months") != null && jsonObject2.get("months") != null) {
					int year1 = jsonObject1.getInt("years");
					int year2 = jsonObject2.getInt("years");
					int month1 = jsonObject1.getInt("months");
					int month2 = jsonObject2.getInt("months");
					if (year1 > year2) {
						return 1;
					} else if (year1 == year2) {
						if (month1 > month2) {
							return 1;
						} else {
							return -1;
						}
					} else {
						return -1;
					}
				} else {
					return 0;
				}
			}
		});
		return Result.csuccess(list);
	}
	
	/**
	 * 卖方配送金额前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerOrderPriceDispatchTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerOrderPriceDispatchProductTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String passportId = getUserId(request);
		String type = jsonObject.getString("type");
		String mongoDbCollection = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sellerId", getUserMemberId(request));
		map.put("type", type);
		if (StringUtils.strIsNotNull(jsonObject.get("productId")) ||
				StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERORDERPRICEDISPATCHPRODUCTTOPTEN;
			if (StringUtils.strIsNotNull(jsonObject.get("productId"))) {
				map.put("productId", jsonObject.get("productId"));
			}
			if (StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
				map.put("productCode", jsonObject.get("productCode"));
			}
		} else {
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERORDERPRICEDISPATCHTOPTEN;
		}
		if (StringUtils.strIsNotNull(jsonObject.get("dispatchId"))) {
			map.put("dispatchId", jsonObject.get("dispatchId"));
		}
 		List<String> projection = new ArrayList<String>();
		projection.add("sellerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
		int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, projection, pageNo, pageSize, sorts));
	}
	/**
	 * 卖方首页订单昨日信息
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerOrderPriceInfoYesterday", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerOrderPriceInfoYesterday(String data, HttpServletRequest request) {
		String passportId = getUserId(request);
		String composeNormalById="";
		if(!userService.isManagerPassport(passportId)){
		composeNormalById=MongoUtil.composeNormalById("passportId", passportId, "passportId",  StatisticsConstant.SellerCollection.SELLERPASSPORTORDERPRICEINFOYESTERDAY);
		}else{
		composeNormalById = MongoUtil.composeNormalById("sellerId", getUserMemberId(request), "sellerId",  StatisticsConstant.SellerCollection.SELLERORDERPRICEINFOYESTERDAY);
		}
		return Result.csuccess(composeNormalById);
	}
	
	/**
	 * 查看今日订单信息
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findSellerTodayTotalOrderInfo", method = RequestMethod.GET)
	@ResponseBody
	public Object findDispatchTodayTotalOrder(String data, HttpServletRequest request) {
		String passportId = getUserId(request);
		Map<String, Object> map = new HashMap<>();
		if (userService.isManagerPassport(passportId)) {
			map = orderService.findSellerTodayTotalOrderInfo(getUserMemberId(request));
		} else {
			map = orderService.findSellerPassportTodayTotalOrderInfo(getUserMemberId(request), passportId);
		}
		if (map == null || map.size() < 1) {
			return Result.csuccess();
		}
		return Result.csuccess(map);
	}
	
	/**
	 * 年度总销售额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerOrderTotalPriceYear", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerOrderTotalPriceYear(String data, HttpServletRequest request) {
		String passportId = getUserId(request);
		String composeNormalById="";
		if(!userService.isManagerPassport(passportId)){
			composeNormalById=MongoUtil.composeNormalById("passportId", passportId, "passportId",  StatisticsConstant.SellerCollection.SELLERPASSPORTORDERTOTALPRICEYEAR);
		}else{
			composeNormalById=MongoUtil.composeNormalById(
					"sellerId", getUserMemberId(request), "sellerId",  StatisticsConstant.SellerCollection.SELLERORDERTOTALPRICEYEAR);
		}
		return Result.csuccess(composeNormalById);
	}
	
	/**
	 * 卖方销售金额前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerOrderPriceProductTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerOrderPriceProductTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		String passportId = getUserId(request);
		String mongoDbCollection = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sellerId", getUserMemberId(request));
		map.put("type", type);
		if (StringUtils.strIsNotNull(jsonObject.get("regionId"))) {
			map.put("region", jsonObject.getString("regionId"));
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERORDERPRICEPRODUCTREGIONTOPTEN;
		} else {
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERORDERPRICEPRODUCTTOPTEN;
		}
		List<String> projection = new ArrayList<String>();
		projection.add("sellerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		if (StringUtils.strIsNotNull(jsonObject.get("ranking"))) {
			sorts.put("ranking", jsonObject.getInt("ranking"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("proportion"))) {
			sorts.put("orderNumber", jsonObject.getInt("proportion"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("price"))) {
			sorts.put("orderNumber", jsonObject.getInt("price"));
		} else {
			sorts.put("sort", 1);
		}
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, projection, 1, 10, sorts));
	}
	
	/**
	 * 卖方下配送买方销售金额前十
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sellerDispatchOrderPriceBuyerTopTen", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerDispatchOrderPriceBuyerTopTen(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		String passportId = getUserId(request);
		String mongoDbCollection = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sellerId", getUserMemberId(request));
		map.put("dispatchId", jsonObject.getString("dispatchId"));
		map.put("type", type);
		if (StringUtils.strIsNotNull(jsonObject.get("regionId"))) {
			map.put("region", jsonObject.getString("regionId"));
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERDISPATCHORDERPRICEBUYERREGIONTOPTEN;
		} else if (StringUtils.strIsNotNull(jsonObject.get("regionId")) || 
				StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
			if (StringUtils.strIsNotNull(jsonObject.get("productId"))) {
				map.put("productId", jsonObject.get("productId"));
			}
			if (StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
				map.put("productCode", jsonObject.get("productCode"));
			}
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERDISPATCHORDERPRICEBUYERPRODUCTTOPTEN;
		} else {
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERDISPATCHORDERPRICEBUYERTOPTEN;
		}
		List<String> projection = new ArrayList<String>();
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("sort", 1);
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
		int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		//System.out.println("map:"+map.toString());
		//System.out.println(changeCollectionName + "~~~" + sorts + "~~~~" + pageNo + "~~~~~" + pageSize);
		List<String> listInfo = MongoUtil.getListInfo(changeCollectionName, map, projection, pageNo, pageSize, sorts);
		//System.out.println("listInfo:"+listInfo.toString());
		return Result.csuccess(listInfo);
	}
	
	/**
	 * 卖方配送企业分析
	 * @author zjh 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @
	 */
	@RequestMapping(value = "/sellerDispatchEnterpriseAnalysis")
	@ResponseBody
	public Object sellerDispatchEnterpriseAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();Map<String, Integer> sorts = new HashMap<String, Integer>();
		String passportId = getUserId(request);
		map.put("sellerId", getUserMemberId(request));
		map.put("time", jsonObject.getString("time"));
		String mongoDbCollection =StatisticsConstant.SellerCollection.SELLERDISPATCHENTERPRISEANALYSIS;
		if (StringUtils.strIsNotNull(jsonObject.get("dispatchName"))) {
			map.put("dispatchName", Pattern.compile("^.*" +jsonObject.getString("dispatchName") +".*$", Pattern.CASE_INSENSITIVE));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productSort"))) {
			sorts.put("totalPrice", Integer.parseInt(jsonObject.get("productSort").toString()));
		}else{
			sorts.put("totalPrice", -1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts));
	}
	
	/**
	 * 卖方配送企业产品分析
	 * @author zjh 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @
	 */
	@RequestMapping(value = "/sellerDispatchEnterpriseProductAnalysis")
	@ResponseBody
	public Object sellerDispatchEnterpriseProductAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();Map<String, Integer> sorts = new HashMap<String, Integer>();
		String passportId = getUserId(request);
		map.put("dispachId", jsonObject.getString("dispachId"));
		map.put("sellerId", jsonObject.getString("sellerId"));
		map.put("time", jsonObject.getString("time"));
		String mongoDbCollection =StatisticsConstant.SellerCollection.SELLERDISPATCHENTERPRISEPRODUCTANALYSIS;
		if (StringUtils.strIsNotNull(jsonObject.get("productName"))) {
			map.put("productName", Pattern.compile("^.*" +jsonObject.getString("productName") +".*$", Pattern.CASE_INSENSITIVE));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
			map.put("productCode", Pattern.compile("^.*" +jsonObject.getString("productCode") +".*$", Pattern.CASE_INSENSITIVE));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productSort"))) {
			sorts.put("totalNum", Integer.parseInt(jsonObject.get("productSort").toString()));
		}else{
			sorts.put("totalNum", -1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts));
	}
	/**
	 * by jiey
	 * 区域 销售分析
	 * 医院销售分析
	 * 
	 */
	@RequestMapping(value = "/sellerRegionSellAnalysis")
	@ResponseBody
	public Object sellerRegionSellAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String passportId = getUserId(request);
		JSONObject jsonObject = dpackData(data);
//		logger.info("进入区域配送----------------");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		//Pattern pattern = Pattern.compile("^.*上药康德乐.*$", Pattern.CASE_INSENSITIVE);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sellerId", getUserMemberId(request));
		//当为time=1（近30天）当为time=2（近7天）
		if(StringUtils.strIsNotNull(jsonObject.get("time"))){
			map.put("time", jsonObject.getString("time"));
		}else{
			map.put("time",'2');
		}
		//sort =1 升序  sort =-1 降序
		if(StringUtils.strIsNotNull(jsonObject.getInt("sort"))){
			sorts.put("maketRatio", jsonObject.getInt("sort"));
		}else{
			sorts.put("maketRatio",1);
		}	
		String mongoDbCollection = "";
		String type = jsonObject.getString("type");
		//String type="dispathcerareainfo";
		if(type.equals("sellerareainfo")){
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERREGIONSELLANALYSIS;
			if(StringUtils.strIsNotNull(jsonObject.get("param"))){
				map.put("regionName", jsonObject.getString("param"));
			}
		}else if(type.equals("sellerbuyerinfo")){
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERHOSPITALSELLANALYSIS;
			if(StringUtils.strIsNotNull(jsonObject.get("param"))){
				map.put("buyerName", Pattern.compile("^.*" +jsonObject.getString("param") +".*$", Pattern.CASE_INSENSITIVE));
			}
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
		int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		//System.out.println("map:"+map.toString());
		//System.out.println(changeCollectionName + "~~~" + sorts + "~~~~" + pageNo + "~~~~~" + pageSize);
		List<String> listInfo = MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts);
		//System.out.println("listInfo:"+listInfo.toString());
		return Result.csuccess(listInfo);
	}
	
	/**
	 * by zjh
	 * 区域 销售药品分析
	 * 医院销售药品分析
	 * 
	 */
	@RequestMapping(value = "/sellerRegionProductSellAnalysis")
	@ResponseBody
	public Object sellerRegionProductSellAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();
		String passportId = getUserId(request);
		boolean isManager = userService.isManagerPassport(passportId);
		if (!isManager) {
			map.put("passportId", passportId);
		}
		map.put("dates", jsonObject.get("dates"));
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		map.put("sellerId", getUserMemberId(request));
		if(StringUtils.strIsNotNull(jsonObject.get("time"))){
			map.put("time", jsonObject.getString("time"));
		}else{
			map.put("time",'2');
		}
		//sort =1 升序  sort =-1 降序
		if(StringUtils.strIsNotNull(jsonObject.get("sort"))){
			sorts.put("maketRatio", jsonObject.getInt("sort"));
		}else{
			sorts.put("maketRatio",1);
		}
		String mongoDbCollection = "";
		if(StringUtils.strIsNotNull(jsonObject.get("productId"))){
			map.put("productId", Pattern.compile("^.*" +jsonObject.getString("productId") +".*$", Pattern.CASE_INSENSITIVE));
		}
		if(StringUtils.strIsNotNull(jsonObject.get("productName"))){
			map.put("productName", Pattern.compile("^.*" +jsonObject.getString("productName") +".*$", Pattern.CASE_INSENSITIVE));
		}
		String type = jsonObject.getString("type");
		 if(type.equals("sellerregionproductsellanalysis")){
			 map.put("regionName", jsonObject.getString("param"));			 
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERREGIONPRODUCTSELLANALYSIS;
		}else if(type.equals("sellerhospitalsellproductanalysis")){
			map.put("buyerName", jsonObject.getString("param"));
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERHOSPITALSELLPRODUCTANALYSIS;
		}

		 int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	     int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	     String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		 
	     return Result.csuccess(MongoUtil.getListInfo(changeCollectionName,  map, null, pageNo, pageSize, sorts));
	}
	/**
	 * 卖方品规销售趋势
	 * 卖方品规区域销售趋势
	 * @param data
	 * @param request
	 * @return
	 * @author zjh
	 */
	@RequestMapping(value = "/sellerProductSellAnalysis", method = RequestMethod.GET)
	@ResponseBody
	public Object sellerProductSellAnalysis(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String type = jsonObject.getString("type");
		String mongoDbCollection = "";
		Map<String, Object> map = new HashMap<String, Object>();
		String passportId = getUserId(request);
		boolean isManager = userService.isManagerPassport(passportId);
		if (!isManager) {
			map.put("passportId", passportId);
		}
		map.put("sellerId", getUserMemberId(request));
		map.put("type", type);
		if (jsonObject.get("regionId") != null) {
			map.put("region", jsonObject.getString("regionId"));
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERPRODUCTREGIONSELLANALYSIS;
		} else {
			mongoDbCollection = StatisticsConstant.SellerCollection.SELLERPRODUCTSELLANALYSIS;
		}
		List<String> projection = new ArrayList<String>();
		projection.add("sellerId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		if (StringUtils.strIsNotNull(jsonObject.get("ranking"))) {
			sorts.put("ranking", jsonObject.getInt("ranking"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("proportion"))) {
			sorts.put("orderNumber", jsonObject.getInt("proportion"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("price"))) {
			sorts.put("orderNumber", jsonObject.getInt("price"));
		} else {
			sorts.put("sort", 1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
		int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
		String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, projection, pageNo, pageSize, sorts));
	}
	
	/**
	 * 卖方品规销售医院产品分析
	 * @author zjh 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @
	 */
	@RequestMapping(value = "/sellerProductSellBuyerAnalysis")
	@ResponseBody
	public Object sellerProductSellBuyerAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();Map<String, Integer> sorts = new HashMap<String, Integer>();
		String passportId = getUserId(request);
		map.put("sellerId", getUserMemberId(request));
		map.put("productCode", jsonObject.getString("productCode"));
		map.put("time", jsonObject.getString("time"));
		String mongoDbCollection =StatisticsConstant.SellerCollection.SELLERPRODUCTSELLBUYERANALYSIS;
		if (StringUtils.strIsNotNull(jsonObject.get("productSort"))) {
			sorts.put("productPrice", Integer.parseInt(jsonObject.get("productSort").toString()));
		}else{
			sorts.put("productPrice", -1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts));
	}
	/**
	 * 卖方品规销售区域产品分析
	 * @author zjh 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @
	 */
	@RequestMapping(value = "/sellerProductSellRegionAnalysis")
	@ResponseBody
	public Object sellerProductSellRegionAnalysis(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();Map<String, Integer> sorts = new HashMap<String, Integer>();
		String passportId = getUserId(request);
		map.put("sellerId", getUserMemberId(request));
		map.put("productCode", jsonObject.getString("productCode"));
		map.put("time", jsonObject.getString("time"));
		String mongoDbCollection =StatisticsConstant.SellerCollection.SELLERPRODUCTSELLREGIONANALYSIS;
		if (StringUtils.strIsNotNull(jsonObject.get("productSort"))) {
			sorts.put("productPrice", Integer.parseInt(jsonObject.get("productSort").toString()));
		}else{
			sorts.put("productPrice", -1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
	    List<String> listInfo = MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts);
		return Result.csuccess(listInfo);
	}
	
	/**
	 * 卖方首页近七天金额
	 * @author zjh 
	 * @param data 
	 * @param request
	 * @param response
	 * @return
	 * @
	 */
	@RequestMapping(value = "/sellerWeekOrderPrice")
	@ResponseBody
	public Object sellerWeekOrderPrice(HttpServletRequest request, HttpServletResponse response) {
		String passportId = getUserId(request);
		String composeNormalById="";
		if(userService.isManagerPassport(passportId)){
			composeNormalById = MongoUtil.composeNormalById("sellerId", getUserMemberId(request), "sellerId",  StatisticsConstant.SellerCollection.SELLERWEEKORDERPRICE);
		}else{
			composeNormalById = MongoUtil.composeNormalById("passportId", passportId, "passportId",  StatisticsConstant.SellerCollection.SELLERPASSPORTWEEKORDERPRICE);
		}
		/*if(StringUtils.strIsNull(composeNormalById)){
			composeNormalById="";
		}*/
		return Result.csuccess(composeNormalById);
	}
	
	/**
	 * 绩效个人
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/sellerPassportSaleTotalPrice")
	@ResponseBody
	public Object dispatchPassportSaleTotalPrice(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();
		String passportId = getUserId(request);
		boolean isManager = userService.isManagerPassport(passportId);
		if (!isManager) {
			map.put("passportId", passportId);
		}
		map.put("dates", jsonObject.get("dates"));
		if (StringUtils.strIsNotNull(jsonObject.get("username"))) {
			map.put("passportName", Pattern.compile("^.*" +jsonObject.getString("username") +".*$", Pattern.CASE_INSENSITIVE));
		}
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		map.put("memberId", getUserMemberId(request));
		if (StringUtils.strIsNotNull(jsonObject.get("passportName"))) {
			sorts.put("passportName",Integer.parseInt(jsonObject.get("passportName").toString()));
		} else if (StringUtils.strIsNotNull(jsonObject.get("orderTotalPrice"))) {
			sorts.put("totalOrderPrice", Integer.parseInt(jsonObject.get("orderTotalPrice").toString()));
		} else {
			sorts.put("totalOrderPrice", -1);
		}
		String mongoDbCollection = StatisticsConstant.SellerCollection.SELLERPASSPORTSALETOTALPRICE;
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts));
	}
	/**
	 * 绩效
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/sellerPassportSaleProductTotalPrice")
	@ResponseBody
	public Object sellerPassportSaleProductTotalPrice(String data, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<String, Object>();
		String passportId = getUserId(request);
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		map.put("passportId", jsonObject.getString("passportId"));
		map.put("dates", jsonObject.getString("dates"));
		if (StringUtils.strIsNotNull(jsonObject.get("productId"))) {
			map.put("productId", Pattern.compile("^.*" +jsonObject.getString("productId") +".*$", Pattern.CASE_INSENSITIVE));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productName"))) {
			map.put("productName", Pattern.compile("^.*" +jsonObject.getString("productName") +".*$", Pattern.CASE_INSENSITIVE));
		}
		String mongoDbCollection = jsonObject.getString("type");
		if (StringUtils.strIsNotNull(jsonObject.get("productSort"))) {
			sorts.put("productName",Integer.parseInt(jsonObject.get("productSort").toString()));
		} else if (StringUtils.strIsNotNull(jsonObject.get("orderTotalPrice"))) {
			sorts.put("orderTotalPrice", Integer.parseInt(jsonObject.get("orderTotalPrice").toString()));
		} else {
			sorts.put("orderTotalPrice", -1);
		}
		int pageNo = jsonObject.get("pageNo") != null ? jsonObject.getInt("pageNo") : 1;
	    int pageSize = jsonObject.get("pageSize") != null ? jsonObject.getInt("pageSize") : 10;
	    String changeCollectionName = changeCollectionName(map, userService.isManagerPassport(passportId),passportId,mongoDbCollection);
		return Result.csuccess(MongoUtil.getListInfo(changeCollectionName, map, null, pageNo, pageSize, sorts));
	}
	
}
